package me.kafeitu.activiti.chapter11;

import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.test.PluggableActivitiTestCase;
import org.activiti.engine.management.TablePage;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ExecutionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.Deployment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 手动触发消息
 *
 * @author henryyan
 */
public class MessageBoundaryEventTest2 extends PluggableActivitiTestCase {
    /**
     * 触发多次消息事件--cancelActivity='false'
     */
    @Deployment(resources = "chapter11/boundaryEvent/messageBoundaryEventNoCancelActivity2.bpmn")
    public void testTriggerManyTimes() throws Exception {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("user", "xian1");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("messageBoundaryEventNoCancelActivity", variables);
        assertNotNull(processInstance);

        System.out.println("11eid=" + processInstance.getId() + ", pid=" + processInstance.getProcessInstanceId());

        // 审核文件任务
        ExecutionQuery executionQuery = runtimeService.createExecutionQuery().messageEventSubscriptionName("MSG_协助处理");
        Execution execution = executionQuery.singleResult();

        // 触发两次
        Map<String, Object> variables2 = new HashMap<String, Object>();
        variables2.put("user", "xian2");
        runtimeService.messageEventReceived("MSG_协助处理", execution.getId(), variables2);
        Map<String, Object> variables3 = new HashMap<String, Object>();
        variables3.put("user", "xian3");
        runtimeService.messageEventReceived("MSG_协助处理", execution.getId(), variables3);

        TablePage listPage = managementService.createTablePageQuery().tableName("ACT_RU_EVENT_SUBSCR").listPage(0, 10);
        List<Map<String, Object>> rows = listPage.getRows();
        for (Map<String, Object> map : rows) {
            Set<Entry<String, Object>> entrySet = map.entrySet();
            for (Entry<String, Object> entry : entrySet) {
                System.out.println(entry.getKey() + " = " + entry.getValue());
            }
        }

        List<Execution> list2 = runtimeService.createExecutionQuery().list();
        for (Execution executionTemp : list2) {
            ExecutionEntity ee = (ExecutionEntity) executionTemp;
            System.out.println("execution: id=" + ee.getId() + ", pid=" + ee.getProcessInstanceId() + ", activityId=" + ee.getActivityId() + ", active="
                    + ee.isActive());
        }

        // 任务到达“县申请”
        // assertEquals(2, taskService.createTaskQuery().taskName("协助处理").count());

        List<Task> list = taskService.createTaskQuery().taskName("县申请").list();
        for (Task task2 : list) {
            System.out.println("task: id=" + task2.getId() + ", executionId=" + task2.getExecutionId() + ", tkey=" + task2.getTaskDefinitionKey() + ", name="
                    + task2.getName());
        }

        // 完成两“县申请”处理任务
        for (Task task2 : list) {
            Map<String, Object> variables11 = new HashMap<String, Object>();
            variables11.put("leader", "shi1");
            taskService.complete(task2.getId(), variables11);
        }
        
        // 回退一次
        List<Task> list4 = taskService.createTaskQuery().taskName("县审核").list();
        
        Map<String, Object> variables8 = new HashMap<String, Object>();
        variables8.put("pass", false);
        taskService.complete(list4.get(0).getId(), variables8);
        
        // 任务回归到审核文件，此时有3个注册了消息名称为“MSG_协助处理”的事件
//        assertEquals(3, executionQuery.count());
//        List<Execution> list3 = executionQuery.list();
//        for (Execution execution2 : list3) {
//            task = taskService.createTaskQuery().executionId(execution2.getId()).singleResult();
//            taskService.complete(task.getId());
//        }

        // 三个任务都完成了，此时整个流程实例结束
        //assertEquals(1, historyService.createHistoricProcessInstanceQuery().finished().count());
        System.out.println("end");
    }

}
